package com.bsoft.gol.hbs.dao;

import ctd.controller.exception.ControllerException;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessAction;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import hcn.base.MsgTemplateMsgType;
import org.atag.dao.util.CustomAliasToEntityMapResultTransformer;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bsoft.gol.hbs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 */
public abstract class MsgTemplateMsgTypeDao extends HibernateSupportDelegateDAO<MsgTemplateMsgType> {
    private static final Logger logger = LoggerFactory.getLogger(MsgTemplateMsgTypeDao.class);

    public MsgTemplateMsgTypeDao() {
        this.setKeyField("msgTemplateMsgTypeId");
    }

    @DAOMethod(sql = "select distinct a  from MsgTemplateMsgType a where a.msgTemplateId=:msgTemplateId  ")
    public abstract MsgTemplateMsgType getMsgTemplateMsgTypeByTemplateId(@DAOParam("msgTemplateId") Long  msgTemplateId);

    /**
     * 消息模板类型信息-删除
     * @param msgTemplateId
     */
    @DAOMethod(sql = " delete from MsgTemplateMsgType  where  msgTemplateId=:msgTemplateId ")
    public abstract void deleteMsgTemplateMsgType(@DAOParam("msgTemplateId") Long msgTemplateId);

    /**
    *
    *@describe得到消息模板的消息类型列表
    */
    public List<MsgTemplateMsgType> getMsgTemplateMsgTypeList(final Long  msgTemplateId) {
        HibernateStatelessResultAction<List<MsgTemplateMsgType>> action =
                new AbstractHibernateStatelessResultAction<List<MsgTemplateMsgType>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuffer stringBuffer = new StringBuffer();
                        stringBuffer.append(" select distinct  msg_template_msg_type_id msgTemplateMsgTypeId,  msg_template_id msgTemplateId, msg_type_key  msgTypeKey from msg_template_msg_type t  where t.msg_template_id=:templateId " );

                        Query query = ss.createSQLQuery(stringBuffer.toString());
                        query.setParameter("templateId", msgTemplateId);
                        query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                        List<Map> list = query.list();
                        List<MsgTemplateMsgType> voList = new ArrayList<MsgTemplateMsgType>();
                        if (CommonUtils.isNotEmpty(list)) {
                            for (Map map : list) {
                                voList.add(ConversionUtils.convert(map, MsgTemplateMsgType.class));
                            }
                        } else {
                            voList = new ArrayList<>();
                        }
                        setResult(voList);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     *
     *@describe 新增消息类型模板
     *@throws
     *@author zhengcb10708
     *@date 2020/9/21 14:14
     */
    public void addMsgTemplateMsgType(final MsgTemplateMsgType msgTemplateMsgType) throws ControllerException {
        HibernateSessionTemplate.instance().execute(new HibernateStatelessAction() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                ss.insert(msgTemplateMsgType);
            }
        });
    }
}
